package com.bjb.vr.common.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjb.vr.common.ao.ResDictionaryAo;
import com.bjb.vr.common.constant.Constants;
import com.bjb.vr.common.constant.ThreadLocalConstant;
import com.bjb.vr.common.dto.ResCascade;
import com.baomidou.mybatisplus.extension.service.IService;
import com.bjb.vr.common.dto.ResDictionary;
import com.bjb.vr.common.mapper.ResCascadeMapper;
import com.bjb.vr.common.utils.GuavaCacheUtil;
import com.bjb.vr.common.utils.ThreadLocalUtil;
import com.bjb.vr.common.vo.ResDictionaryVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author admin
* @description 针对表【res_cascade】的数据库操作Service
* @createDate 2022-10-27 15:09:28
*/
@Service
public class ResCascadeService extends ServiceImpl<ResCascadeMapper, ResCascade> {

	private static final String PRIMARY_CODE = "1008";
	private static final String UNION = "01";
	private static final String PRIMARY_TYPE = "0";
	private static final String JUNIOR_TYPE = "1";

	@Autowired
	ResCascadeMapper resCascadeMapper;
	/**
	 * 联动查询
	 *
	 * @param resDictionaryAo
	 * @return
	 */
	public List<ResDictionaryVo> queryDictionaryByPeriodLink(ResDictionaryAo resDictionaryAo) {
		// 如果不传code，那就只查对应的type类型
		if (StringUtils.isBlank(resDictionaryAo.getDictCode())) {
			// 如果type为01，则查询type为0和1的并集
			if (UNION.equals(resDictionaryAo.getDictType())) {
				List<ResDictionaryVo> dictionaryList = resCascadeMapper.queryUnionData();
				return dictionaryList;
			} else {
				List<ResDictionaryVo> resDictionaryList = getResDictionaryList(resDictionaryAo.getDictType());
				return resDictionaryList;
			}
		} else {
			// 如果传1008，则查询小学阶段的学科，否则查询中学阶段的学科
			if (PRIMARY_CODE.equals(resDictionaryAo.getDictCode())) {
				List<ResDictionaryVo> resDictionaryList = getResDictionaryList(PRIMARY_TYPE);
				return resDictionaryList;
			} else {
				List<ResDictionaryVo> resDictionaryList = getResDictionaryList(JUNIOR_TYPE);
				return resDictionaryList;
			}
		}
	}


	/**
	 * 根据字典类型查询
	 *
	 * @param type
	 * @return
	 */
	public List<ResDictionaryVo> getResDictionaryList(String type) {
		ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, true);
		try {
			LambdaQueryWrapper<ResCascade> queryWrapper = Wrappers.lambdaQuery(ResCascade.class);
			queryWrapper.eq(ResCascade::getLevelType, type);
			List<ResCascade> cascadeList = this.list(queryWrapper);
			List<ResDictionaryVo> resDictionaryVoList = convertPoToVo(cascadeList);
			return resDictionaryVoList;
		} catch (Exception e) {
			log.error(">>> 查询缓存失败, 将查询数据库 >>>",e);

		}finally {
			ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, false);
		}
		return null;
	}

	/**
	 * 根据字典类型查询党建菜单
	 *
	 * @param type
	 * @return
	 */
	public List<ResDictionaryVo> getResDictionaryDJList(String type) {
		ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, true);
		try {
			LambdaQueryWrapper<ResCascade> queryWrapper = Wrappers.lambdaQuery(ResCascade.class);
			queryWrapper.eq(ResCascade::getLevelType, type);
			List<ResCascade> cascadeList = this.list(queryWrapper);
			List<ResDictionaryVo> resDictionaryVoList = convertDJPoToVo(cascadeList);
			return resDictionaryVoList;
		} catch (Exception e) {
			log.error(">>> 查询缓存失败, 将查询数据库 >>>",e);

		}finally {
			ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, false);
		}
		return null;
	}

	/**
	 * 数据类型转换
	 *
	 * @param
	 * @return
	 */
	private List<ResDictionaryVo> convertPoToVo(List<ResCascade> cascadeList) {
		List<ResDictionaryVo> resDictionaryVos = new ArrayList<>();
		cascadeList.stream().forEach(cascade -> {
			ResDictionaryVo resDictionaryVo = new ResDictionaryVo();
			resDictionaryVo.setDictCode(cascade.getSubjectCode());
			resDictionaryVo.setDictName(cascade.getSubjectName());
			resDictionaryVo.setDictStatus(1);
			resDictionaryVo.setDictOrder(cascade.getDictOrder());
			resDictionaryVos.add(resDictionaryVo);
		});
		return resDictionaryVos;
	}

	/**
	 * 数据类型转换
	 *
	 * @param
	 * @return
	 */
	private List<ResDictionaryVo> convertDJPoToVo(List<ResCascade> cascadeList) {
		List<ResDictionaryVo> resDictionaryVos = new ArrayList<>();
		cascadeList.stream().forEach(cascade -> {
			ResDictionaryVo resDictionaryVo = new ResDictionaryVo();
			if (cascade.getLevelType().equals("2")){
				resDictionaryVo.setDictCode(cascade.getLevelCode());
				resDictionaryVo.setDictName(cascade.getLevelName());
			}else {
				resDictionaryVo.setDictCode(cascade.getSubjectCode());
				resDictionaryVo.setDictName(cascade.getSubjectName());
			}
			resDictionaryVo.setDictStatus(1);
			resDictionaryVo.setDictOrder(cascade.getDictOrder());
			resDictionaryVos.add(resDictionaryVo);
		});
		return resDictionaryVos;
	}
}
